﻿using Authentication.Model.Model;
using Common.Data.Repository;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Data.Repository
{
    public class ObjectRepository : GenericRepository
    {
        private DbSet<ApplicationObject> DBSet { get { return ((AuthenticationEntities)this.DbContext).Objects; } }

        public IEnumerable<ApplicationObject> SearchObjects(out int totalRecord, Guid applicationKey, string name = null, string parentIdentifier = null, int pagesize = 0, int pageindex = 0)
        {
            var appkey = applicationKey.ToString().ToUpper();
            var querry = from o in DBSet where o.ApplicationKey.ToUpper() == appkey select o;

            if (!string.IsNullOrWhiteSpace(name))
            {
                querry = querry.Where(x => x.ObjectNameEnglish.ToUpper().Contains(name.Trim().ToUpper()) || x.ObjectNameLocal.ToUpper().Contains(name.Trim().ToUpper()));
            }

            if (!string.IsNullOrWhiteSpace(parentIdentifier))
            {
                querry = querry.Where(x => x.ParentId.ToUpper().Equals(parentIdentifier.Trim().ToUpper()));
            }
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }

            return querry.ToList();
        }
    }
}
